package com.sunday.gateway.core.filter.limit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.support.ConfigurationService;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * keyResolver是实现接口的 Bean
 *
 * @author zsy
 * @see org.springframework.cloud.gateway.config.GatewayRedisAutoConfiguration#redisRateLimiter(org.springframework.data.redis.core.ReactiveStringRedisTemplate, org.springframework.data.redis.core.script.RedisScript, ConfigurationService)
 * @see org.springframework.cloud.gateway.config.GatewayAutoConfiguration#principalNameKeyResolver()
 * @see org.springframework.cloud.gateway.config.GatewayAutoConfiguration#requestRateLimiterGatewayFilterFactory(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter, KeyResolver)
 * @since 2024/3/7
 */

@Slf4j
@Component
public class RedisRateLimiterKeyResolver implements KeyResolver {
    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        //获取请求IP，作为速率的判定key
        String remoteAddress = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();
//        log.info("{}", remoteAddress);
        return Mono.just(remoteAddress);
    }
}
